package com.simple.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simple.common.PageResult;
import com.simple.dto.BillDTO;
import com.simple.dto.FundDTO;
import com.simple.mapper.FundMapper;
import com.simple.pojo.Bill;
import com.simple.pojo.Fund;
import com.simple.service.FundService;
import com.simple.utils.BeanConverterUtil;
import com.simple.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Time;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName FundServiceImpl
 * @Author SimpleTree
 **/
@Service
public class FundServiceImpl implements FundService {

    @Resource
    private FundMapper fundMapper;
    @Autowired
    private BeanConverterUtil beanConverterUtil;

    @Override
    public PageResult getFunds(FundDTO fundDTO) {
        PageHelper.startPage(fundDTO.getPageNum(), fundDTO.getPageSize());

        List<Fund> funds = fundMapper.getFunds(fundDTO.getUserId(),fundDTO.getFundName(),-1);
        PageInfo<Fund> pageInfo = new PageInfo<>(funds);

        List<FundDTO> fundDTOs = beanConverterUtil.toFundDTO(funds);

        Map<String, List<FundDTO>> fundNameMap = fundDTOs.stream().collect(Collectors.groupingBy(FundDTO::getFundName));

        for (Map.Entry<String, List<FundDTO>> entry : fundNameMap.entrySet()) {
            List<FundDTO> value = entry.getValue();
            Collections.sort(value, (o1, o2) -> o1.getUpdateTime().before(o2.getUpdateTime()) ? -1 : 1);

            BigDecimal currentMoney = new BigDecimal(value.get(0).getBuyMoney()).subtract(new BigDecimal(value.get(0).getSellMoney() + ""));
            for (FundDTO dto : value) {
                currentMoney = currentMoney.add(new BigDecimal(dto.getProfit() + ""));
                dto.setCurrentMoney(currentMoney.doubleValue());
            }
        }

        return new PageResult(pageInfo.getTotal(),pageInfo.getPageNum(),fundDTOs);
    }

    @Override
    public FundDTO getFundById(Integer id) {
        Fund fund = fundMapper.selectByPrimaryKey(id);
        return beanConverterUtil.toFundDTO(fund);
    }

    @Override
    public void saveFund(FundDTO fundDTO) {
        Fund fund = beanConverterUtil.toFund(fundDTO);
        fund.setCreateTime(new Date());
        fund.setBuyTime(TimeUtil.parse(fundDTO.getBuyTime()));
        fund.setSellTime(TimeUtil.parse(fundDTO.getSellTime()));
        fundMapper.insert(fund);
    }

    @Override
    public void updateFund(FundDTO fundDTO) {
        //更新操作直接插入新数据，
        Fund fund = beanConverterUtil.toFund(fundDTO);
        fund.setId(null);
        fund.setCreateTime(new Date());
        fund.setUpdateTime(new Date());
        fund.setBuyTime(TimeUtil.parse(fundDTO.getBuyTime()));
        fund.setSellTime(TimeUtil.parse(fundDTO.getSellTime()));
        fund.setProfit(fundDTO.getProfit());
        fundMapper.insert(fund);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        fundMapper.deleteByIds(ids);
    }

    @Override
    public void updateDel(FundDTO fundDTO) {
        Fund fund = new Fund();
        fund.setId(fundDTO.getId());
        fund.setDel(fundDTO.getDel());
        fundMapper.updateByPrimaryKeySelective(fund);
    }

    @Override
    public List<Fund> getFundsOfMonth(Integer userId) {
        return fundMapper.getFundsOfMonth(userId);
    }
}
